#include "shipChooser.h"
#include <QDir>
#include <QPushButton>
#include <QDebug>

ShipChooser::ShipChooser()
{
  mUI.setupUi(this);

  QStringList namefilter;
  namefilter << "*.lua";

  QDir::Filters filter(QDir::Files);

  QDir luaships("lua/ships");
  QStringList luashipfiles(luaships.entryList(namefilter, filter));
  QDir userdataships("userdata/ships");
  QStringList userdatashipfiles(userdataships.entryList(namefilter, filter));
  luashipfiles.append(userdatashipfiles);
  luashipfiles.replaceInStrings(".lua", "", Qt::CaseInsensitive);

  mUI.shipList->addItems(luashipfiles);
  connect(mUI.shipList, SIGNAL(clicked(QModelIndex)),
          this, SLOT(itemSelected()));

  QPushButton* ok(mUI.buttonBox->button(QDialogButtonBox::Ok));
  if (ok)
  {
    ok->setDisabled(true);
  }
}

QString ShipChooser::ship() const
{
  return mUI.shipList->selectedItems().front()->text();
}

void ShipChooser::itemSelected()
{
  QPushButton* ok(mUI.buttonBox->button(QDialogButtonBox::Ok));
  if (ok)
  {
    ok->setEnabled(true);
  }
}
